package com.test.prolenders.activity_prolenders;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;

import com.test.prolenders.analytics_prolenders.timezone_prolenders.TimeZoneUtil_prolenders;
import com.test.prolenders.analytics_prolenders.util_prolenders.AnalyticsUtil_prolenders;
import com.test.prolenders.R;
import com.test.prolenders.activity_prolenders.ln_prolenders.MainActivity_prolenders;
import com.test.prolenders.base_prolenders.BaseActivity_prolenders;
import com.test.prolenders.base_prolenders.BaseApplication_prolenders;
import com.test.prolenders.bean_prolenders.userinfo_prolenders.LoginInfo_prolenders;
import com.test.prolenders.config_prolenders.PermissionConfig_prolenders;
import com.test.prolenders.util_prolenders.UserInfoUtil_prolenders;
import com.test.prolenders.util_prolenders.http_prolenders.HttpUtil_prolenders;

import org.json.JSONException;
import org.json.JSONObject;


public class LauncherActivity_prolenders extends BaseActivity_prolenders {

    private boolean isFirstStart = true;

    private ActivityResultLauncher<Intent> requestPermissionArl;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_launcher_prolenders);
        page = "LAUNCH";

        requestPermissionArl = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), this::afterPermission);

        findViewById(R.id.al_start).setOnClickListener(v -> startProcess());

    }

    @Override
    protected void onResume() {
        super.onResume();
        if (isFirstStart) startProcess();
    }

    private void afterPermission(ActivityResult result){
        if (result.getResultCode()==RESULT_OK) onTZResponse();
        else if (result.getResultCode()==RESULT_OUT_APP) finish();
    }

    private void startProcess(){
        //1.获取时区配置
        HttpUtil_prolenders.getInstance().requestTzConfig(this, TimeZoneUtil_prolenders.INSTANCE.getTimeZoneID(),this::onTZResponse);
    }

    public void onTZResponse(){
        //2.检查权限
        if(!PermissionConfig_prolenders.checkPhonestatePermission(this) || (isFirstStart&&!PermissionConfig_prolenders.checkSmsPermission(this))){
            isFirstStart = false;
            requestPermissionArl.launch(new Intent(this, PermissionActivity_prolenders.class));
            return;
        }

        AnalyticsUtil_prolenders.INSTANCE.init(BaseApplication_prolenders.getInstant());

        HttpUtil_prolenders.getInstance().requestAppSInfo(this,this::onAppShowInfoResponse);
    }

    public void onAppShowInfoResponse(){

        //自动登录
        if (UserInfoUtil_prolenders.getInstance().didLogin()){
            try {
                JSONObject object = new JSONObject();
                object.put("token", UserInfoUtil_prolenders.getInstance().getToken());
                object.put("userId", UserInfoUtil_prolenders.getInstance().getUserId());
                object.put("type", UserInfoUtil_prolenders.LOGIN_TYPE_OLD);
                object.put("isAdSource", UserInfoUtil_prolenders.getInstance().getIsAdSource() ? "YES":"NO");
                LoginInfo_prolenders.parse(object);

                AnalyticsUtil_prolenders.INSTANCE.saveFirebaseAndFacebookS2SInfo();
                HttpUtil_prolenders.getInstance().userProcess(this,this::onUserProcessResponse);
                return;
            }catch (JSONException e){
                e.printStackTrace();
            }
        }

        Intent intent = new Intent(this, LoginActivity_prolenders.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        startActivity(intent);
        finish();
    }

    public void onUserProcessResponse(){
        startActivity(new Intent(this, MainActivity_prolenders.class));
        finish();
    }

}